09. Building a P Controller
Programming a Proportional Controller
Now lets get started and talk about the goal of this quiz and what your expected results are to look like!
In the following quiz we will be building a proportional controller for regulating the altitude of a quadrotor. You will need to follow along with the interactive comments in the code in order to build out the controller.
You want to shoot to have your output look like so:
Where you can see that we are oscillating closer to the set point.
Be sure to spend sometime playing around with Kp (the proportional constant) and see its effects one the altitude controller.
Ask your self the questions:
- What happens when Kp is large?
- What happens when Kp is small?
- Do we reach our desired goal?
- How does control effort vary with different Kp values?
- How does the steady state offset and overshoot vary with different Kp values?
You can see below the desired control effort for this controller. Does it look efficient?
Keep these questions in mind as we progress through these quizzes and observe the positive and negative aspects of having or not having various controller components.
Final Notes
As a final note, you will be working controller from a proportional controller all the way up to an advanced PID controller in order to create the script that you will be using in your exercise. So be sure to get comfortable with the code base your building upon and try to understand everything that is happening!
Good luck!
Start Quiz:
import numpy as np
import matplotlib.pyplot as plt
from p_controller import P_Controller
from quad1d_eom import ydot
##################################################################################
##################################################################################
# This code is what will be executed and what will produce your results
# For this quiz you need to set kp below to your desired value
# Then modify p_controller.py to build out your P controller
kp = 0.76
# Note that kp needs to be set to 0.76 in order to pass the project
# You are encouraged to change Kp in order to observe the effects
# What happens when Kp is really small?
# What happens when Kp is really large?
# Do we notice anything relating Kp and the control effort?
# Observe the steady state offset and the percent overshoot!
##################################################################################
##################################################################################
# Simulation parameters
N = 500 # number of simultion points
t0 = 0 # starting time, (sec)
tf = 30 # end time, (sec)
time = np.linspace(t0, tf, N)
dt = time[1] - time[0] # delta t, (sec)
##################################################################################
# Core simulation code
# Inital conditions (i.e., initial state vector)
y = [0, 0]
#y[0] = initial altitude, (m)
#y[1] = initial speed, (m/s)
# Initialize array to store values
soln = np.zeros((len(time),len(y)))
# Create instance of P_Controller class
p = P_Controller()
# Set the Kp value of the controller
p.setKP(kp)
# Set altitude target
r = 10 # meters
p.setTarget(r)
# Simulate quadrotor motion
j = 0 # dummy counter
for t in time:
# Evaluate state at next time point
y = ydot(y,t,p)
# Store results
soln[j,:] = y
j += 1
##################################################################################
# Plot results
SP = np.ones_like(time)*r # altitude set point
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax1.plot(time, soln[:,0],time,SP,'--')
ax1.set_xlabel('Time, (sec)')
ax1.set_ylabel('Altitude, (m)')
ax2 = fig.add_subplot(212)
ax2.plot(time, soln[:,1])
ax2.set_xlabel('Time, (sec)')
ax2.set_ylabel('Speed, (m/s)')
plt.tight_layout()
plt.show()
fig2 = plt.figure()
ax3 = fig2.add_subplot(111)
ax3.plot(time, p.u_p, label='u_p', linewidth=3, color = 'red')
ax3.set_xlabel('Time, (sec)')
ax3.set_ylabel('Control Effort')
h, l = ax3.get_legend_handles_labels()
ax3.legend(h, l)
plt.tight_layout()
plt.show()
##################
y0 = soln[:,0] #altitude
rise_time_index = np.argmax(y0>r)
RT = time[rise_time_index]
print("The rise time is {0:.3f} seconds".format(RT))
OS = (np.max(y0) - r)/r*100
if OS < 0:
OS = 0
print("The percent overshoot is {0:.1f}%".format(OS))
print ("The steady state offset at 30 seconds is {0:.3f} meters".format(abs(soln[-1,0]-r)))
##################################################################################
# Your goal is to follow the comments and complete the the tasks asked of you.
# Take this time to try and understand the workings of the empty class structure.
# The following quizzes will assume a understanding of a working class strucutre.
#
# Good luck designing your proportional controller!
#
##################################################################################
class P_Controller:
def __init__(self, kp = 0.0, start_time = 0):
# The P controller can be initalized with a specific kp value
self.kp_ = float(kp)
# Create internal class variables for
# set_point_ and set it to 0.0, and start_time_
# and set it to the start_time variable.
########################################
########################################
# Store last timestamp
self.last_timestamp_ = 0.0
# Control effort history
self.u_p = [0]
# Set the altitude set point
def setTarget(self, target):
self.set_point_ = float(target)
def setKP(self, kp):
# Set the internal kp_ value with the provided variable
# See setTarget if you are confused on how to do so
########################################
########################################
def update(self, measured_value, timestamp):
# Calculate delta_time using the last_timestamp_
# and the provided timestamp argument
########################################
########################################
if delta_time == 0:
# Delta time is zero
return 0
# Calculate the error as the differnce between
# the set_point_ and the measured_value
########################################
########################################
# Set the last_timestamp_ to current timestamp
########################################
########################################
# Calculate the proportional error here. Be sure to access the
# the internal Kp class variable
########################################
p = None
########################################
# Set the control effort
# u is the sum of all your errors. In this case it is just
# the proportional error.
########################################
u = None
########################################
# Here we are storing the control effort history for post control
# observations.
self.u_p.append(p)
return u
import numpy as np
import matplotlib.pyplot as plt
from p_controller import P_Controller
##################################################################################
## DO NOT MODIFY ANY PORTION OF THIS FILE
# This file represents the dynamical equations of motion for the 1D quadrotor
##################################################################################
def ydot(y, t, p):
''' Returns the state vector at the next time-step
Parameters:
----------
y(k) = state vector, a length 2 list
= [altitude, speed]
t = time, (sec)
pid = instance of the PIDController class
Returns
-------
y(k+1) = [y[0], y[1]] = y(k) + ydot*dt
'''
# Model state
y0 = y[0] # altitude, (m)
y1 = y[1] # speed, (m/s)
# Model parameters
g = -9.81 # gravity, m/s/s
m = 1.54 # quadrotor mass, kg
c = 10.0 # electro-mechanical transmission constant
# time step, (sec)
dt = t - p.last_timestamp_
# Control effort
u = p.update(y0,t)
### State derivatives
# if altitude = 0
if (y0 <= 0.):
# if control input, u <= gravity, vehicle stays at rest on the ground
# this prevents quadrotor from "falling" through the ground when thrust is
# too small.
if u <= np.absolute(g*m/c):
y0dot = 0.
y1dot = 0.
else: # else if u > gravity and quadrotor accelerates upwards
y0dot = y1
y1dot = g + c/m*u - 0.75*y1
else: # otherwise quadrotor is already in the air
y0dot = y1
y1dot = g + c/m*u - 0.75*y1
y0 += y0dot*dt
y1 += y1dot*dt
return [y0, y1]
##################################################################################
# Your goal is to follow the comments and complete the the tasks asked of you.
# Take this time to try and understand the workings of the empty class structure.
# The following quizzes will assume a understanding of a working class strucutre.
#
# Good luck designing your proportional controller!
#
##################################################################################
class P_Controller:
def __init__(self, kp = 0.0, start_time = 0):
# The P controller can be initalized with a specific kp value
self.kp_ = float(kp)
# Create internal class variables for
# set_point_ and set it to 0.0, and start_time_
# and set it to the start_time variable.
########################################
self.set_point_ = 0.0
self.start_time_ = start_time
########################################
# Store last timestamp
self.last_timestamp_ = 0.0
# Control effort history
self.u_p = [0]
# Set the altitude set point
def setTarget(self, target):
self.set_point_ = float(target)
def setKP(self, kp):
# Set the internal kp_ value with the provided variable
# See setTarget if you are confused on how to do so
########################################
self.kp_ = float(kp)
########################################
def update(self, measured_value, timestamp):
# Calculate delta_time using the last_timestamp_
# and the provided timestamp argument
########################################
delta_time = timestamp - self.last_timestamp_
########################################
if delta_time == 0:
# Delta time is zero
return 0
# Calculate the error as the differnce between
# the set_point_ and the measured_value
########################################
error = self.set_point_ - measured_value
########################################
# Set the last_timestamp_ to current timestamp
########################################
self.last_timestamp_ = timestamp
########################################
# Calculate the proportional error here. Be sure to access the
# the internal Kp class variable
########################################
p = self.kp_ * error
########################################
# Set the control effort
# u is the sum of all your errors. In this case it is just
# the proportional error.
########################################
u = p
########################################
# Here we are storing the control effort history for post control
# observations.
self.u_p.append(p)
return u
Reflect
QUESTION:
Take some time after you have played with the code above to reflect on these questions:
- What happens when Kp is large?
- What happens when Kp is small?
- Do we reach our desired goal?
- How does control effort vary with different Kp values?
- How does the steady state offset and overshoot vary with different Kp values?
ANSWER:
There is no desired answer to this question but hopefully taking time to reflect on these questions will give you a deeper understanding of the proportional "knob"!
Quick check!
SOLUTION:
- Increase